

#include "stdafx.h"
//#include "stdafx.h"
#include"SqList.h"

int a[]={3,5,8,11};
int b[]={2,6,8,9,11,15,20};

Status MergeList(SqList La,SqList Lb,SqList *Lc);

int test_merge_main()
{
    int i;
    SqList La,Lb,Lc;

    InitList(&La);
    for(i=0;i<  (sizeof(a)/sizeof(int));i++)
    {	
	if(OK != ListInsert(&La,i+1,a[i]))
	    return FALSE;
    }
    PrintList(La,"La");

    InitList(&Lb);
    for(i=0;i<(sizeof(b)/sizeof(int));i++)
    {	
	if(OK != ListInsert(&Lb,i+1,b[i]))
	    return FALSE;
    }
    PrintList(Lb,"Lb");

    InitList(&Lc);
    PrintList(Lc,"Lc");

    MergeList(La,Lb,&Lc);
    PrintList(Lc,"Lc");
}

Status MergeList(SqList La,SqList Lb,SqList *Lc)
{
    int i=0,j=0,k=0;
    while(i<La.length && j<Lb.length)
    {
			if(La.base[i] < Lb.base[j])
			{
				if(OK != ListInsert(Lc,Lc->length+1,La.base[i]))
				return FALSE;
				i++;
			}

			else if(La.base[i] > Lb.base[j])
			{
				if(OK != ListInsert(Lc,Lc->length+1,Lb.base[j]))
				return FALSE;
				j++;
			}

			else if(La.base[i] == Lb.base[j])
			{
				if(OK != ListInsert(Lc,Lc->length+1,La.base[i]))
				return FALSE;

				i++;
				j++;
			}

    }


		while(i<La.length)
		{
		if(OK != ListInsert(Lc,Lc->length+1,La.base[i]))
			return FALSE;
		i++;
		}

		while(j<Lb.length)
		{
		if(OK != ListInsert(Lc,Lc->length+1,Lb.base[j]))
			return FALSE;
		j++;
		}

}


